package com.yungu.swift.order.utils;

import com.yungu.swift.utils.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author : cuixiuyin
 * @date : 2019/4/9
 */
public class CommonUtils {

    /**
     * 播报预约时间的处理
     *
     * @return string
     */
    public static String getTimeTodayOrTomorrow(Date deparTime) {
        LocalDateTime localDeparDateTime = deparTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();

        LocalDateTime todayMax = LocalDate.now().atTime(LocalTime.MAX);
        LocalDateTime tomorrowMax = todayMax.plusDays(1);
        LocalDateTime theDayAfterTomorrowMax = tomorrowMax.plusDays(1);

        if (todayMax.isAfter(localDeparDateTime)) {
            return "今天" + localDeparDateTime.format(DateTimeFormatter.ofPattern(" HH:mm"));
        }
        if (tomorrowMax.isAfter(localDeparDateTime)) {
            return "明天" + localDeparDateTime.format(DateTimeFormatter.ofPattern(" HH:mm"));
        }
        if (theDayAfterTomorrowMax.isAfter(localDeparDateTime)) {
            return "后天" + localDeparDateTime.format(DateTimeFormatter.ofPattern(" HH:mm"));
        }
        return localDeparDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
    }


    /**
     * 正则获取详细地址中的区县
     *
     * @param source
     * @return
     */
    public static String getCounty(String source) {
        String result = "";
        if (StringUtils.isNotBlank(source)) {
            String regex = "市(.+?(区|县))";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(source);
            while (matcher.find()) {
                result = matcher.group(1);
            }
        }
        return result;
    }
}
